package com.example.demo;

import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo.fragment.DataFragment;
import com.example.demo.fragment.EnvironmentFragment;
import com.example.demo.fragment.EquipmentFragment;
import com.example.demo.fragment.SettingFragment;


public class IndexActivity extends AppCompatActivity {
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        initToolBar();
        initTabhost();
    }
    public void initToolBar() {
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("欢迎："+getIntent().getStringExtra("loginname"));
        // 如果要修改toolbar标题栏文字，可添加如下语句
        //toolbar.setTitle(getIntent().getStringExtra("loginName"));
        // toolbar.setTitle("智慧农业") ;
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.toolbar_exit:
                        finish();
                        break;
                    case R.id.toolbar_logout:
                        Intent intent = new Intent(IndexActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                }
                return false;
            }
        }) ;
    }
    //设置toolbar要显示的菜单项，如果没有重写此方法，不会出现菜单项
    private FragmentTabHost tabHost;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu , menu);
        return super.onCreateOptionsMenu(menu);
    };
    private void initTabhost() {
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this , getSupportFragmentManager() , R.id.realcontent);
        // 为FragmentTabHost设置标签标题和对应的标签页
        tabHost.addTab(
                tabHost.newTabSpec("Environment").setIndicator(setTabContent("实时环境",R.drawable.footer_envi_selector))
                , EnvironmentFragment.class // 设置点击该标签时加载的Fragment类
                ,null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.addTab(
                tabHost.newTabSpec("Data").setIndicator(setTabContent("数据分析",R.drawable.footer_data_selector))
                , DataFragment.class // 设置点击该标签时加载的Fragment类
                ,null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.addTab(
                tabHost.newTabSpec("Equipment").setIndicator(setTabContent("设备控制",R.drawable.footer_equipment_selector))
                , EquipmentFragment.class // 设置点击该标签时加载的Fragment类
                ,null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.addTab(
                tabHost.newTabSpec("Setting").setIndicator(setTabContent("参数设置",R.drawable.footer_setting_selector))
                , SettingFragment.class // 设置点击该标签时加载的Fragment类
                ,null// Fragment初始化时如果需要传参，通过该参数传递
        );

    }
    //设置选项卡的布局
    public View setTabContent(String label, int imageId) {
        //引入选项卡的布局
        View view = LayoutInflater.from(this).inflate(R.layout.tabcontent, null);
        TextView textView = view.findViewById(R.id.footer_tv);
        textView.setText(label);
        ImageView imageView = view.findViewById(R.id.footer_iv);
        imageView.setImageResource(imageId);
        return view;
    }
}
